package com.beidu.lottery.call.constant;

import com.beidu.lottery.call.common.BaseContextHandler;
import com.beidu.lottery.call.utils.LocalDateUtils;

/**
 * 缓存键常量
 *
 * @author 北渡
 */
public class CacheKey {

    // 统计-访问量-PV
    public static final String STATS_ACCESS_PV = "stats:access:pv:";

    // 统计-访问量-UV
    public static final String STATS_ACCESS_UV = "stats:access:uv:";

    // 统计-访问量（PV、UV）入库锁
    public static final String STATS_ACCESS_LOCK = "stats:access:lock";

    // MQ消费记录-分布式锁
    public static final String MQ_LOG_LOCK = "mq:log:lock";

    // 请求标记-报名提交
    public static final String REQUEST_REG = "request:reg:";

    // 请求标记-投票提交
    public static final String REQUEST_VOTE = "request:vote:";

    // 序列-报名序列（每日）
    public static final String SEQUENCE_REG = "sequence:reg";

    // 序列-投票序列（每日）
    public static final String SEQUENCE_VOTE = "sequence:vote";

    // 序列-MQ主键序列（每日）
    public static final String SEQUENCE_MQ = "sequence:mq";

    // 计数器-活动剩余报名名额
    public static final String COUNTER_REG_QUOTA = "counter:reg:";

    // 计数器-用户剩余投票次数
    public static final String COUNTER_VOTE_TIMES = "counter:vote:";

    // 计数器-用户剩余抽奖次数
    public static final String COUNTER_LOTTERY_TIMES = "counter:lottery:";

    // 业务看板-日均数据
    public static final String DASHBOARD_DAILY = "dashboard:daily:";

    // 业务看板-日均数据-分布式锁
    public static final String DASHBOARD_DAILY_LOCK = "dashboard:daily:lock";

    // 业务看板-月均数据
    public static final String DASHBOARD_MONTHLY = "dashboard:monthly:";

    // 业务看板-月均数据-分布式锁
    public static final String DASHBOARD_MONTHLY_LOCK = "dashboard:monthly:lock";

    // 业务看板-最新活动
    public static final String DASHBOARD_ACTIVITY_LATEST = "dashboard:activity:latest:";

    // 业务看板-最热活动
    public static final String DASHBOARD_ACTIVITY_HOT = "dashboard:activity:hot:";

    // 活动规则-报名
    public static final String RULE_REG = "rule:reg:";

    // 活动规则-投票
    public static final String RULE_VOTE = "rule:vote:";

    // 活动规则-抽奖
    public static final String RULE_LOTTERY = "rule:lottery:";

    // 热点数据（通用前缀）
    public static final String HOTSPOT = "hotspot:";

    // 商品库存
    public static final String INVENTORY = "inventory:";

    /**
     * 活动信息缓存空值 Key
     */
    public static final String ACTIVITY_IS_NULL_KEY = "activity_is_null:%s";

    /**
     * 生成报名活动的剩余名额计数Key
     *
     * @param id 活动id
     * @return key组成方式："counter:reg:活动id
     */
    public static String genRegCounter(Integer id) {
        return CacheKey.COUNTER_REG_QUOTA + id;
    }

    /**
     * 生成投票活动的剩余票数计数Key
     *
     * @param id 活动id
     * @return key组成方式："counter:vote:活动id:用户id
     */
    public static String genVoteCounter(Integer id) {
        return CacheKey.COUNTER_VOTE_TIMES + id + ":" + BaseContextHandler.getUserId();
    }

    /**
     * 生成抽奖活动的剩余票数计数Key
     *
     * @param id 活动id
     * @return key组成方式："counter:lottery:活动id:用户id
     */
    public static String genLotteryCounter(Integer id) {
        return CacheKey.COUNTER_LOTTERY_TIMES + id + ":" + BaseContextHandler.getUserId();
    }

    /**
     * 生成投票活动的商品剩余库存Key
     *
     * @param id1 活动id
     * @param id2 关联id（库存表）
     * @return key组成方式："counter:lottery:活动id:关联id
     */
    public static String genInventoryKey(Integer id1, Integer id2) {
        return CacheKey.INVENTORY + id1 + ":" + id2;
    }

    /**
     * 生成抽奖活动的剩余中奖名额Key
     *
     * @param id 活动id
     * @return key组成方式："hotspot:活动id:quota
     */
    public static String genWinQuotaKey(Integer id) {
        return CacheKey.HOTSPOT + id + ":quota";
    }

    /**
     * 生成投票选项Key
     *
     * @param activityId 活动ID
     * @param groupId    分组ID
     * @return key组成方式：hotspot:{活动id}:{分组id}:options
     */
    public static String genOptionsKey(Integer activityId, Integer groupId) {
        return CacheKey.HOTSPOT + activityId + ":" + groupId + ":options";
    }

    /**
     * 生成最新活动数据Key
     *
     * @return key组成方式：dashboard:activity:latest:{租户编码}
     */
    public static String genLatestActivityKey() {
        return CacheKey.DASHBOARD_ACTIVITY_LATEST + BaseContextHandler.getTenant();
    }

    /**
     * 生成最热活动数据Key
     *
     * @return key组成方式：dashboard:activity:hot:{租户编码}
     */
    public static String genHotActivityKey() {
        return CacheKey.DASHBOARD_ACTIVITY_HOT + BaseContextHandler.getTenant();
    }

    /**
     * 生成活动PV浏览KEY
     *
     * @param id 活动id
     * @return key组成方式："stats:access:pv:浏览日期:租户Code:活动id
     */
    public static String genAccessPvKey(Integer id) {
        return CacheKey.STATS_ACCESS_PV + LocalDateUtils.getDay() + ":" + BaseContextHandler.getTenant() + ":" + id;
    }

    /**
     * 生成活动UV浏览KEY
     *
     * @param id 活动id
     * @return key组成方式："stats:access:pv:浏览日期:租户Code:活动id:IP地址
     */
    public static String genAccessUvKey(Integer id, String ip) {
        return CacheKey.STATS_ACCESS_UV + LocalDateUtils.getDay() + ":" + BaseContextHandler.getTenant() + ":" + id + ":" + ip;
    }

}
